package com.ytjj.qmyx.users.controller;

import com.aliyun.oss.OSSClient;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.FileResponse;
import com.ytjj.common.utils.AliyunOSSClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description 图床
 *
 * @Version 1.0
 * 
 */

@RestController
@Slf4j
@RequestMapping("/oss")
public class AliyunOssController extends BaseController {

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public CommonResult getImgUrl(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return CommonResult.failed("文件不存在");
        }
        // 初始化OssClient
        OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
        log.info("开始上传文件 =====>" + file.getOriginalFilename());
        FileResponse fileResponse = AliyunOSSClientUtil.uploadObject2OSS(ossClient, file);
        if (fileResponse == null) {
            return CommonResult.failed("文件上传失败");
        }
        return CommonResult.success(fileResponse);
    }

    /**
     * 上传文件+文字水印
     * @param file
     * @return
     */
    @PostMapping("/wmTextUpload")
    public CommonResult wmTextUpload(@RequestParam("file") MultipartFile file){
        return CommonResult.success(AliyunOSSClientUtil.wmTextUpload(file,"XXXX优品"));
    }

    /**
     * 上传文件+图片水印
     * @param goalFile
     * @param wmFile
     * @return
     */
    @PostMapping("/wmImgUpload ")
    public CommonResult wmImgUpload(@RequestParam("goalFile") MultipartFile goalFile, @RequestParam("wmFile") MultipartFile wmFile){
        return CommonResult.success(AliyunOSSClientUtil.wmImgUpload(goalFile, wmFile));
    }

}
